class Student < ActiveRecord::Base
  has_many :registrations
  has_one :user, :as => :usable

  validates :first_name, :presence => true
  validates :surname, :presence => true

  def full_name
    [first_name, surname].compact.join(' ')
  end

  def active_registration
    self.registrations.detect {|r| r.timeslot.active? && r.cancel_time.nil? }
  end

  def has_active_registration?    
    !self.active_registration.nil?
  end

  def driving_lessons
    not_cancelled = self.registrations.select {|r| r.cancel_time.nil? && !r.driving_lesson.nil? }
    not_cancelled.collect {|r| r.driving_lesson }
  end

  def cancellation_reports
    cancelled = self.registrations.select {|r| !r.cancellation_report.nil? }
    reports = cancelled.collect {|r| r.cancellation_report }
    reports.uniq
  end
end
